package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.BaoBean;
import com.wnhz.work.yz.bean.YanShouDaiBean;
import com.wnhz.work.yz.bean.YanShouDaiDetailsBean;
import com.wnhz.work.yz.utils.GlideImageLoader;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YanShou2BaoActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private LinearLayout status1, status2;
    private View line1, line2;
    private TextView submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_dai88);
        //todo 创建数据库表
        //  findViewById(R.id.top).setVisibility(View.GONE);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("报检");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
//        submit = (TextView) findViewById(R.id.submit);
//        submit.setText("分配");
//        submit.setVisibility(View.VISIBLE);
//        findViewById(R.id.submit).setOnClickListener(this);
        status1 = (LinearLayout) findViewById(R.id.status1);
        status2 = (LinearLayout) findViewById(R.id.status2);
        status1.setOnClickListener(this);
        status2.setOnClickListener(this);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    }

    private List<YanShouDaiBean.InfoEntity> info1 = new ArrayList<>();

    @Override
    protected void onResume() {
        upLoad(type);
        super.onResume();
    }

    private int type = 0;

    private void upLoad(final int type) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("keywords", "");
        map.put("type", type);

        showDialog();
        XUtil.Post(Url.DECORATE_WAITINGREPORTLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouDaiBean bean = gson.fromJson(result, YanShouDaiBean.class);
                        info1 = bean.getInfo();
                        initRecycler(recycler, info1, type);
                        recycler.setVisibility(View.VISIBLE);
                        //// TODO: 2017/6/19 给第二个表添加数据
                        // initDaData(info1);

                    } else {
                        recycler.setVisibility(View.GONE);
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private BaseRVAdapter adapter;

    private void initRecycler(RecyclerView recycler, final List<YanShouDaiBean.InfoEntity> info, final int type) {
        adapter = new BaseRVAdapter(this, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_dai;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {

                if ("1".equals(info.get(position).getIs_tranfer())) {
                    holder.getView(R.id.ll_select).setVisibility(View.GONE);
                    holder.getView(R.id.fenpei).setVisibility(View.VISIBLE);
                    holder.setTextView(R.id.elsetime, info.get(position).getTranfer_time());
                    holder.getView(R.id.istrue).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            doWhat(position, info.get(position).getOrderid(), 1);
                        }
                    });
                    holder.getView(R.id.isfalse).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            doWhat(position, info.get(position).getOrderid(), 2);

                        }
                    });


                } else {
                    holder.getView(R.id.fenpei).setVisibility(View.GONE);
                    if (info.get(position).getOpen() == 1) {
                        holder.getView(R.id.ll_select).setVisibility(View.VISIBLE);
                    } else {
                        holder.getView(R.id.ll_select).setVisibility(View.GONE);
                    }

                    if (info.get(position).getSelect() == 1) {
                        holder.getImageView(R.id.select).setImageResource(R.mipmap.ic_choose_li);
                    } else {
                        holder.getImageView(R.id.select).setImageResource(R.mipmap.ic_choose_nor);
                    }
                    holder.getView(R.id.ll_select).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            info.get(position).setSelect(info.get(position).getSelect() == 1 ? 0 : 1);
                            notifyDataSetChanged();
                        }
                    });
                }


                if (info.get(position).getImg() != null && info.get(position).getImg().size() > 0) {
                    //   x.image().bind(img, info.getImg().get(0));
                    Banner img = holder.getView(R.id.img2);
                    img.setImageLoader(new GlideImageLoader());
                    //设置图片集合
                    img.setImages(info.get(position).getImg());
                    img.setBannerStyle(BannerConfig.NOT_INDICATOR);
                    //banner设置方法全部调用完毕时最后调用
                    img.start();
                }

                holder.getTextView(R.id.name).setText(info.get(position).getProject_name());
                holder.getTextView(R.id.hetonghao).setText("" + info.get(position).getBudget_no());
                holder.getTextView(R.id.time).setText("" + info.get(position).getCheck_time()
                );
                holder.getTextView(R.id.address).setText("" + info.get(position).getAddress());
                holder.getTextView(R.id.anzhuang).setText("安装队：" + info.get(position).getInstall());
                holder.getTextView(R.id.or).setText("总套数：" + info.get(position).getAll_nums() + "套");
                if (type == 0)
                    holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showDialogv7("是否报检？", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    getDetails(info.get(position).getOrderid());
                                }
                            });
                        }
                    });
                else {
                    holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getDetails2(info.get(position).getOrderid());

                        }
                    });
                }


            }

        };
        recycler.setAdapter(adapter);

    }

    private void getDetails2(final String orderid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_REPORTDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
//                        MyToast(info);
//                        onResume();
                        Gson gson = new Gson();
                        BaoBean bean = gson.fromJson(result, BaoBean.class);
                        startActivity(new Intent(YanShou2BaoActivity.this, BaoXQActivity.class).putExtra("bean", bean));
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });
    }

    private void getDetails(final String orderid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_REPORT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
                        MyToast(info);
                        onResume();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });
    }


    private void doWhat(final int position, String orderid, final int i) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        map.put("status", i);
        showDialog();

        XUtil.Post(Url.DECORATE_DOWHATTOORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        MyToast(info);
                        if (i == 1) {
                            info1.get(position).setIs_tranfer("0");
                            adapter.notifyDataSetChanged();
                        } else {
                            info1.remove(position);
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }


    private void getDetails(final int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", info1.get(position).getOrderid());
        showDialog();

        XUtil.Post(Url.DECORATE_WAITINGCHECKDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouDaiDetailsBean bean = gson.fromJson(result, YanShouDaiDetailsBean.class);
                        startActivity(new Intent(YanShou2BaoActivity.this, YanShou2GaiChanPinActivity.class).putExtra("bean", bean));


                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

//            case R.id.ll_search:
//                startActivity(new Intent(this, YanShouDaiSearchActivity.class));
//
//                break;
//            分配
//            case R.id.submit:
//                if ("分配".equals(submit.getText())) {
//                    submit.setText("确认");
//                    for (int i = 0; i < info1.size(); i++) {
//                        info1.get(i).setOpen(1);
//                    }
//                    adapter.notifyDataSetChanged();
//                } else {
//                    StringBuffer sb = new StringBuffer("-");
//                    for (int i = 0; i < info1.size(); i++) {
//                        if (info1.get(i).getSelect() == 1) {
//                            sb.append(info1.get(i).getOrderid() + "-");
//                        }
//                    }
//                    submit.setText("分配");
//                    if ("-".equals(sb.toString())) {
//                        for (int i = 0; i < info1.size(); i++) {
//                            info1.get(i).setOpen(0);
//                        }
//                        adapter.notifyDataSetChanged();
//                        return;
//                    }
//                    startActivity(new Intent(YanShou2BaoActivity.this, YanShouYuanLieBiaoActivity.class).putExtra("orderid", sb.toString()));
//                    for (int i = 0; i < info1.size(); i++) {
//                        info1.get(i).setOpen(0);
//                        info1.get(i).setSelect(0);
//                    }
//                    adapter.notifyDataSetChanged();
//                }
//
//                break;
            case R.id.status1:
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.GONE);
                // submit.setVisibility(View.VISIBLE);
                //  submit.setText("分配");
                type = 0;
                upLoad(type);
                break;
            case R.id.status2:
                //  submit.setVisibility(View.GONE);
                line2.setVisibility(View.VISIBLE);
                line1.setVisibility(View.GONE);
                type = 1;
                upLoad(type);
                break;
        }
    }
}
